package com.stedu.mall.orderservice.controller;

import com.stedu.mall.common.bean.Cart;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.CartService;
import com.stedu.mall.common.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/cart")

public class CartController {
    @Autowired
    private CartService cartService;
    @PostMapping("/{goodsId}")
    public RespBean insert(@PathVariable("goodsId") Integer goodsId, @RequestHeader("token") String token){
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        //创建购物车对象
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setGoodsId(goodsId);
        cartService.insert(cart);
        return RespBean.ok("添加购物车成功");
    }
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id,@RequestHeader("token") String token) throws SteduException {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        return cartService.delete(id,userId) ? RespBean.ok("删除购物车成功") : RespBean.error("删除购物车失败");

    }
    @PutMapping
    public RespBean update(@RequestBody Cart cart,@RequestHeader("token") String token) throws SteduException {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        return cartService.update(cart,userId) ? RespBean.ok("更新购物车成功") : RespBean.error("更新购物车失败");
    }
    //查询已登录用户所有的购物车信息
    @GetMapping
    public RespBean selectCartList(@RequestHeader("token") String token){
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        Cart condition = new Cart();
        condition.setUserId(userId);
        List<Cart> cartList = cartService.serach(condition);
        return RespBean.ok("查询购物车列表成功",cartList);
    }
    @GetMapping("/{id}")
    public RespBean selectCartById(@PathVariable("id") Integer id){
        Cart cart = cartService.selectById(id);
        return RespBean.ok("",cart);
    }
//    根据userId 删除购物车所有商品
    @DeleteMapping("/userId")
    public RespBean deleteAll(@RequestHeader("token") String token){
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        cartService.deleteByUserId(userId);
        return RespBean.ok("清空购物车成功" );
    }
    @DeleteMapping
    public RespBean deleteByIds(@RequestBody Map<String, ArrayList<Integer>> data, @RequestHeader("token") String token){
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        ArrayList<Integer> ids = data.get("ids");
        //删除
        cartService.deleteByIds(ids,userId);
        return RespBean.ok("删除购物车成功");
    }
    //批量查询
    @GetMapping("/ids")
    private RespBean selectByIds(Integer[] cartIds, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");
        List<Cart> cartList = cartService.selectByIds(cartIds, userId);
        return RespBean.ok("查询成功", cartList);
    }
}
